package com.cn.kabalifeuser.ui.my.activity;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.my.adapter.PagerAdapter;
import com.cn.kabalifeuser.ui.my.bean.CouponListBean;
import com.cn.kabalifeuser.ui.my.bean.CouponTypeListBean;
import com.cn.kabalifeuser.ui.my.fragment.CouponFragments;
import com.cn.kabalifeuser.ui.my.fragment.CouponPresenter;
import com.cn.kabalifeuser.ui.my.fragment.CouponView;
import com.cn.kabalifeuser.utils.RxToast;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 晓林 on 2020/9/7.
 * function: 领劵中心
 */
public class CouponActivity extends IBaseActivity<CouponView, CouponPresenter> implements CouponView, OnTabSelectListener {
    @BindView(R.id.slitab)
    SlidingTabLayout slitab;
    @BindView(R.id.vp_type)
    ViewPager vpType;
    List<String> mList = new ArrayList<>();
    PagerAdapter mAdapter;
    private List<Fragment> mFragments = new ArrayList<>();

    @Override
    protected CouponPresenter createPresenter() {
        return new CouponPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_coupon;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBar("领劵中心");
//        slitab.setCurrentTab(sign);
        mPresenter.onCouponTypeData(LoginHelper.getLonginData().getResult());
    }


    @Override
    public void onTabSelect(int position) {

    }

    @Override
    public void onTabReselect(int position) {

    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return getMe();
    }

    @Override
    public void onCouponTypeSuccess(CouponTypeListBean bean) {

        for (int i = 0; i < bean.getResult().size(); i++) {
            mList.add(bean.getResult().get(i).getName());
            mFragments.add(CouponFragments.getInstance(bean.getResult().get(i).getId()));
        }
        mAdapter = new PagerAdapter(getSupportFragmentManager(), mList, mFragments);
        vpType.setAdapter(mAdapter);
        vpType.setOffscreenPageLimit((mList.size() - 1));
        slitab.setViewPager(vpType);
        slitab.setOnTabSelectListener(this);
    }

    @Override
    public void onCouponListSuccess(CouponListBean bean) {

    }

    @Override
    public void onReceiveCouponSuccess(MsgBean bean) {

    }
}
